package com.windgo.controller.admin;

import com.github.pagehelper.PageInfo;
import com.windgo.bean.MarketStorage;
import com.windgo.bean.common.BasePageInfo;
import com.windgo.bean.common.BaseRespVo;
import com.windgo.bean.common.CommonData;
import com.windgo.bean.vo.RespInfoVO;
import com.windgo.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

/**
 * @author doublec
 * @since 2022/10/11 19:41
 */
@RestController
@RequestMapping("admin/storage")
public class AdminStorageController {
    @Autowired
    AdminService adminService;

    /**
     * 显示全部对象信息
     * @param basePageInfo
     * @return
     */
    @RequestMapping("list")
    public BaseRespVo<CommonData<MarketStorage>> listStorage(BasePageInfo basePageInfo,String key,String name) {
        CommonData<MarketStorage> commonData = new CommonData<>();
        PageInfo<MarketStorage> pageInfo = adminService.listStorage(basePageInfo,key,name);
        int total = Long.valueOf(pageInfo.getTotal()).intValue();
        int pages = pageInfo.getPages();
        Integer limit = basePageInfo.getLimit();
        int pageNum = pageInfo.getPageNum();
        List<MarketStorage> list = pageInfo.getList();
        commonData.setTotal(total);
        commonData.setPages(pages);
        commonData.setLimit(limit);
        commonData.setPage(pageNum);
        commonData.setList(list);
        return BaseRespVo.ok(commonData);
    }

    /**
     * 新建对象
     * @param file
     * @param request
     * @return
     * @throws IOException
     */
    @RequestMapping("create")
    public BaseRespVo<CommonData<MarketStorage>> createStorage(MultipartFile file, HttpServletRequest request) throws IOException {
        //获取文件的名称转为hash值作为key
        UUID uuid = UUID.randomUUID();
        String uuidString = uuid.toString();
        String uuidName = uuidString.replace("-", "");
        String fileName = file.getName();
        //获取到原始文件名称（name）
        String originalFilename = file.getOriginalFilename();
        int index = originalFilename.indexOf(".");
        String endStr = originalFilename.substring(index);
        fileName = uuidName  + fileName + endStr;
        //int hashCode = fileName.hashCode();
        //String hexString = Integer.toHexString(hashCode);
        //获取文件的类型（type）
        String contentType = file.getContentType();
        //获取文件的大小（size）
        long size = file.getSize();
        int fileSize = Long.valueOf(size).intValue();
        // //String requestURI = request.getRequestURI();
        // StringBuffer url = request.getRequestURL();
        // String urlString = url.toString();
        // int adminIndex = urlString.lastIndexOf("admin");
        // String uriPath = urlString.substring(0, adminIndex);
        String host = request.getHeader("Host");
        String pathName = "http://"+host+"/wx/storage/fetch/"+fileName;
        //获取到文件的访问链接（就是保存的地址）
        File file1 = new File("E:\\image",fileName);
        file.transferTo(file1);
        MarketStorage marketStorage = new MarketStorage(fileName, originalFilename, contentType, fileSize, pathName, false);
        int number = adminService.createStorage(marketStorage);
        Integer id = marketStorage.getId();
        MarketStorage fileMarketStorage = adminService.queryMarketStorage(id);
        return BaseRespVo.ok(fileMarketStorage);
    }

    /**
     * 删除对象（逻辑删除）
     * @param marketStorage
     * @return
     */
    @RequestMapping("delete")
    @Transactional
    public RespInfoVO deleteStorage(@RequestBody MarketStorage marketStorage) {
        int number = adminService.deleteStorage(marketStorage);
        RespInfoVO respVo = new RespInfoVO(0, "成功");
        return respVo;
    }

    @RequestMapping("update")
    public BaseRespVo<MarketStorage> updateStorage(@RequestBody MarketStorage marketStorage){
        int number = adminService.updateStorage(marketStorage);
        if (number == 1) {
            MarketStorage storage = adminService.queryMarketStorage(marketStorage.getId());
            return BaseRespVo.ok(storage);
        }
        return BaseRespVo.invalidData("修改失败");
    }
}
